package com.cpi.basilos.silo.helper;

import java.util.Calendar;

import org.apache.log4j.Logger;

import com.cpi.basilos.silo.exception.SiloException;
import com.cpi.basilos.silo.view.SiloDatesView;
import com.cpi.basilos.silo.view.SiloFiltersView;
import com.cpi.user.view.UserView;
import com.cs.common.exception.CSException;
import com.cs.common.utils.TimeUtils;

public class SiloHelper {
	private static org.apache.log4j.Logger logger = Logger.getLogger(SiloHelper.class);
	public SiloFiltersView createFilterToService(SiloDatesView siloDatesView, UserView userView) throws SiloException{
		SiloFiltersView siloFiltersView = null;
		Calendar calInit = null;
		Calendar calEnd = null;
		try{
			siloFiltersView = new SiloFiltersView();
			siloFiltersView.setWarehouseId(userView.getWarehouseId());
			calInit = Calendar.getInstance();
			calInit.setTimeInMillis(TimeUtils.convertStringToSQLDate(siloDatesView.getInitDate()).getTime());
			siloFiltersView.setInitDate(calInit);
			calEnd = Calendar.getInstance();
			calEnd.setTimeInMillis(TimeUtils.convertStringToSQLDate(siloDatesView.getEndDate()).getTime());
			siloFiltersView.setEndDate(calEnd);
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			siloException = new SiloException(exception , CSException.LAYER_HELPER,CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloFiltersView;
	}
	
	public SiloDatesView createDefaultFilter() throws SiloException{
		SiloDatesView siloDatesView = null;
		Calendar calToday = null;
		try{
			siloDatesView = new SiloDatesView();
			calToday = Calendar.getInstance();
			siloDatesView.setEndDate(TimeUtils.convertJavaDateToString(calToday.getTime()));
			calToday.add(Calendar.DATE, -1);
			siloDatesView.setInitDate(TimeUtils.convertJavaDateToString(calToday.getTime()));
			
			siloDatesView.setEndDate("30/10/2009");
			siloDatesView.setInitDate("29/10/2009");
		}catch(Exception exception){
			SiloException siloException = null;
			exception.printStackTrace(System.out);
			logger.error(exception);
			siloException = new SiloException(exception , CSException.LAYER_HELPER,CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloDatesView;
	}
}